﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Configuration;
using CompanyBussiness;

namespace Website.admin.Control
{
    public partial class cCreateContact : System.Web.UI.UserControl
    {
        NewsManagement newsMana = new NewsManagement();
        LanguagesManagement languageMana = new LanguagesManagement();
        ServicesBasic serviceBasic = new ServicesBasic();
        CompanyBussiness.Contact contact;
        CompanyBussiness.Language language;
        CompanyBussiness.New news;
        CheckValid cv = new CheckValid();
        string strFileName = "";
        bool AddOrUp = false;
        int STRID;
        string error = "";


        protected void Page_Load(object sender, EventArgs e)
        {

            if (Request.Cookies[CONST.COOKIES_LANGUAGE] == null)
            {
                Session[CONST.SESSION_LANGUAGE] = "";
                language = languageMana.GetLanguageByCode("vi-VN");
            }
            else
            {
                Session[CONST.SESSION_LANGUAGE] = Request.Cookies[CONST.COOKIES_LANGUAGE][CONST.COOKIES_LANGUAGE_ID];
                language = languageMana.GetLanguageByID(int.Parse(Session[CONST.SESSION_LANGUAGE].ToString()));
            }

            if (Request[CONST.QUERYSTRING_CONTACT] == null )
            {
                AddOrUp = false;
                btnDelete.Visible = false;
                lbTypeAction.Text = "Phòng ban nhận email liên hệ";
            }
            else
            {
                AddOrUp = true;
                STRID = int.Parse(Request[CONST.QUERYSTRING_CONTACT]);
                this.LoadForm();
                lbTypeAction.Text = "Phòng ban nhận email liên hệ";
            }
        }

        protected bool GetData()
        {
            if (txtRoom.Text.Trim() != "")
            {
                contact.ContactName = txtRoom.Text;
            }
            else
            {
                return false;
            }

            if (txtEmail.Text.Trim() != "")
            {

                contact.ContactEmail = txtEmail.Text;
            }
            else
            {
                return false;
            }

           
            return true;
        }

        protected void LoadForm()
        {
            try
            {
                txtRoom.Text = contact.ContactName;

                if (contact.ContactEmail != null)
                    txtEmail.Text = contact.ContactEmail;
                else
                    txtEmail.Text = "";

            }
            catch (Exception)
            {
                //fail.Visible = true;
                //lbError.Text = "Có lỗi trong khi Load dữ liệu . Bạn hãy kiểm tra lại.";
            }

        }

        protected void FormatForm()
        {
            try
            {
                if (AddOrUp == false)
                {
                    txtEmail.Text = "";
                    txtRoom.Text = "";
                    
                }
                else
                {
                    this.LoadForm();
                }
            }
            catch (Exception)
            { }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (AddOrUp == false)
                {
                    #region Addnew

                    contact = new CompanyBussiness.Contact();

                    if (GetData() == false)
                        return;

                    //tao moi phong ban nhan email lien he
                    int idOut = serviceBasic.CreateAddress(contact);

                    Response.Redirect("Administrator.aspx?" + CONST.ACTION + "=Settings");

                    //success.Visible = true;
                    //lbSuccess.Text = "Bạn đã thêm mới bài viết !";
                    //fail.Visible = false;

                    this.FormatForm();

                    #endregion
                }
                else
                {
                    #region Update

                    contact = serviceBasic.GetContactByID(STRID);

                    if (GetData() == false)
                        return;

                    serviceBasic.UpdateRoom((int)contact.ContactID, contact.ContactName, contact.ContactEmail);

                    Response.Redirect("Administrator.aspx?" + CONST.ACTION + "=Settings");
                    //success.Visible = true;
                    //lbSuccess.Text = "Bạn đã cập thành công !";
                    //fail.Visible = false;

                    this.LoadForm();

                    #endregion
                }
            }
            catch (Exception ex)
            {
                //fail.Visible = true;
                //lbError.Text = "Có lỗi trong khi sử lý dữ liệu . Bạn hãy kiểm tra lại.";
            }   
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.FormatForm();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                contact = serviceBasic.GetContactByID(STRID);
                serviceBasic.DeleteContact(contact.ContactID);

                Response.Redirect("Administrator.aspx?" + CONST.ACTION + "=Settings");
            }
            catch (Exception)
            {
                //fail.Visible = true;
                //lbError.Text = "Có lỗi trong khi sử lý dữ liệu . Bạn hãy kiểm tra lại.";
            }
        }
    }
}